<?php
namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\Pool\MagicPool;


class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
        // 载入项目 Conf 文件夹中所有的配置文件
        self::loadConf(EASYSWOOLE_ROOT . '/Config');
    }

    public static function loadConf($ConfPath)
    {

        $Conf  = Config::getInstance();
        $file = new \EasySwoole\Utility\File();
        $files = $file->scanDirectory($ConfPath);
        var_dump($files);
        if (!is_array($files["files"])) {
            return;
        }
        foreach ($files["files"] as $file) {
            $data = require_once $file;
            $Conf->setConf(strtolower(basename($file, '.php')), (array)$data);
        }
    }

    public static function mainServerCreate(EventRegister $register)
    {

        //连接mysql
        $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('database'));
        DbManager::getInstance()->addConnection(new Connection($config));
//
        // TODO: Implement mainServerCreate() method.

    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}